package com.pai.service.Department;

import com.pai.dao.BaseDao;
import com.pai.dao.Department.DepartmentDao;
import com.pai.dao.Department.DepartmentDaoImpl;
import com.pai.dao.role.RoleDao;
import com.pai.dao.role.RoleDaoImpl;
import com.pai.entity.AppRole;
import com.pai.entity.Department;
import com.pai.service.Role.RoleService;
import com.pai.service.Role.RoleServiceImpl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class DepartmentServiceImpl implements DepartmentService{
    private DepartmentDao theDao;  //部门dao
    private RoleDao roleDao;  //角色dao

    public DepartmentServiceImpl() {
        this.theDao = new DepartmentDaoImpl();
        this.roleDao = new RoleDaoImpl();
    }

    /**
     * 获取列表
     * @return
     */
    @Override
    public List<Department> getList() {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Department> resultList = new ArrayList<>();
        try {
            resultList = theDao.getList(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取列表
     * @param depType 部门类型： 0：学院 1：学工处 2：班级
     * @return
     */
    @Override
    public List<Department> getList(String depType) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Department> resultList = new ArrayList<>();
        try {
            resultList = theDao.getList(connection, depType);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取班级部门列表
     * @param depId  父级部门id
     * @return
     */
    @Override
    public List<Department> getDepClassList(String depId) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Department> resultList = new ArrayList<>();
        try {
            resultList = theDao.getDepClassList(connection, depId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 根据负责人id获取部门列表
     * @param principalId 部门负责人Id
     * @return
     */
    @Override
    public List<Department> getListByPrincipalId(String principalId) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Department> resultList = new ArrayList<>();
        try {
            resultList = theDao.getListByPrincipalId(connection, principalId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取班级下拉选项
     * @param depId  部门id
     * @param roleId  角色id
     * @param pensonId  人员id
     * @return
     */
    @Override
    public List<Department> getClassOptions(String depId,  String roleId, String pensonId) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Department> resultList = new ArrayList<>();
        Department reqDepartment = null;
        AppRole appRole = null;
        try {
            reqDepartment = theDao.getItemDetail(connection, depId);
            appRole = roleDao.getItemDetail(connection, roleId);
            //如果发起请求的部门为学生工作处
            if(reqDepartment.getDepType() == 1){
                resultList = theDao.getList(connection, "2");  //获取全部的班级
            }
            //如果发起请求的人的部门为学院部门
            else {
                //如果是学院部门二级管理员
                if(appRole.getRoleName().equals("二级部门管理员")){
                    resultList = theDao.getDepClassList(connection, depId);
                }
                //如果是辅导员
                else if(appRole.getRoleName().equals("辅导员")){
                    resultList = theDao.getListByPrincipalId(connection, pensonId);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取项详情
     * @param id
     * @return
     */
    @Override
    public Department getItemDetail(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        Department item = new Department();
        try {
            item = theDao.getItemDetail(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return item;
    }

    /**
     * 获取项详情
     * @param depName  部门名称
     * @param depType  部门类型
     * @return
     * @throws Exception
     */
    @Override
    public Department getItemDetail(String depName, String depType) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        Department item = new Department();
        try {
            item = theDao.getItemDetail(connection, depName, depType);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return item;
    }

    /**
     * 新增
     * @param item
     * @return
     */
    @Override
    public boolean add(Department item) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.add(connection, item) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }



    /**
     * 更新
     * @param item
     * @return
     */
    @Override
    public boolean update(Department item) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.update(connection, item) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public boolean delete(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.delete(connection, id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }
}
